package com.example.Day02.POJO;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

@WebServlet("/ser02")
public class registerServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) {
        String uid = req.getParameter("id");
        String upassword = req.getParameter("password");

        //连接数据库
        try {
            //实例化Driver，使用Class.forName目的是装载一个类，并对其实例化操作
            Class.forName("com.mysql.jdbc.Driver");
            //设置连接  DriverManager.get  (url,user,psw)
            Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/User","root","Zhang1221?");
            //执行数据库（Statement为java执行数据库的重要接口）
            Statement st = con.createStatement();
            //编写sql语句
            String sql="insert into User(id, password) VALUES ('"+uid+"','"+upassword+"')";
           // PreparedStatement pst =con.prepareStatement(sql);
            //pst.setString(1,uid);
            //pst.setString(2,upassword);
            //设置一个计数器，主要用于计数update次数
            int cout=st.executeUpdate(sql);
            //System.out.println("这是pst:"+pst);
           // System.out.println("这是rs"+rs);
            if(cout==1){
                System.out.println("注册成功");
                resp.sendRedirect("Login.jsp");
            }else {
                System.out.println("注册失败");
                resp.sendRedirect("register.jsp");
            }
            //ResultSet rst =pst.executeQuery();
            con.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException | IOException throwables) {
            throwables.printStackTrace();
        }

    }
}
